﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dominio.Entidades;

public partial class Default2 : System.Web.UI.Page
{
    VacacionesDataContext db = new VacacionesDataContext();

    protected void Page_Init(object sender, EventArgs e)
    {

        //http://www.codeproject.com/Articles/134614/Way-To-Know-Which-Control-Has-Raised-PostBack
        //http://weblogs.asp.net/zeeshanhirani/archive/2008/04/20/how-to-find-which-control-raised-postback.aspx
        if (IsPostBack)
        {
            string ControlID = string.Empty;
            if (!String.IsNullOrEmpty(Request.Form["__EVENTTARGET"]))
            {
                ControlID = Page.Request.Params["__EVENTTARGET"];
                Control postbackControl = Page.FindControl(ControlID);
            }
        }

            GridView1.AutoGenerateColumns = false;

            DateTime fecha = DateTime.Parse("01/02/2012");
            var dias = DateTime.DaysInMonth(fecha.Year, fecha.Month);

            for (var i = 1; i <= DateTime.DaysInMonth(fecha.Year, fecha.Month); i++)
            {
                BoundField bfield = new BoundField();

                bfield.DataField = "_" + i.ToString();

                bfield.HeaderText = i.ToString();

                GridView1.Columns.Add(bfield);
            }

            switch (dias)
            {
                case 31:
                    GridView1.DataSource = db.V_Mes31s.ToList();
                    break;

                case 30:
                    GridView1.DataSource = db.V_Mes30s.ToList();
                    break;

                case 29:
                    GridView1.DataSource = db.V_Mes29s.ToList();
                    break;

                case 28:
                    GridView1.DataSource = db.V_Mes28s.ToList();
                    break;

            }

            //GridView1.DataSource= mes;
            GridView1.DataBind();
    }


    protected void Page_Load(object sender, EventArgs e)
    {/*
        if (!IsPostBack)
        {
            GridView1.AutoGenerateColumns = false;

            DateTime fecha = DateTime.Parse("01/02/2012");
            var dias = DateTime.DaysInMonth(fecha.Year, fecha.Month);

            for (var i = 1; i <= DateTime.DaysInMonth(fecha.Year, fecha.Month); i++)
            {
                BoundField bfield = new BoundField();

                bfield.DataField = "_" + i.ToString();

                bfield.HeaderText = i.ToString();

                GridView1.Columns.Add(bfield);
            }

            switch (dias)
            {
                case 31:
                    GridView1.DataSource = db.V_Mes31s.ToList();
                    break;

                case 30:
                    GridView1.DataSource = db.V_Mes30s.ToList();
                    break;

                case 29:
                    GridView1.DataSource = db.V_Mes29s.ToList();
                    break;

                case 28:
                    GridView1.DataSource = db.V_Mes28s.ToList();
                    break;

            }

            //GridView1.DataSource= mes;
            GridView1.DataBind();
        }*/

    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
          if (e.Row.RowType == DataControlRowType.DataRow)

          for (var i = 0; i < e.Row.Cells.Count; i++) 
            {
                 if (e.Row.Cells[i].Text == "V")
                 {
                     LinkButton lb = new LinkButton();
                     lb.Text = "V";
                     lb.CommandName = "ApproveVacation";
                     lb.ForeColor = System.Drawing.Color.White;
                     lb.BackColor = System.Drawing.Color.Red;
                     lb.Command += LinkButton_Command;
                     e.Row.Cells[i].Controls.Add(lb);

                     Image v = new Image { ImageUrl = "~/img/question.png" };
                     e.Row.Cells[i].Controls.Add(v);
                 }
                
            }
       /* /*
                if (e.Row.Cells[e.Row.RowIndex].Text == "V")
                {
                    LinkButton lb = new LinkButton();
                    lb.Text = "V";
                    lb.CommandName = "ApproveVacation";
                    lb.ForeColor = System.Drawing.Color.White;
                    lb.BackColor = System.Drawing.Color.Red;
                    lb.Command += LinkButton_Command;
                    e.Row.Cells[e.Row.RowIndex].Controls.Add(lb);

                    e.Row.Cells[e.Row.RowIndex].ForeColor = System.Drawing.Color.White;
                    e.Row.Cells[e.Row.RowIndex].BackColor = System.Drawing.Color.Red;
                }*/
  }

    protected void LinkButton_Command(object sender, CommandEventArgs e)
    {
        //GridView1.DataSource = null;
        if (e.CommandName == "ApproveVacation")
        {

            LinkButton lb = (LinkButton)sender;
            lb.ForeColor = System.Drawing.Color.White;
            lb.BackColor = System.Drawing.Color.Blue;
        }
    }

    protected void GridView1_DataBound(object sender, EventArgs e)
    {
      /*  foreach (GridViewRow row in GridView1.Rows)
        {
            
            if (row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lb = new LinkButton();
                lb.Text = "Approve";
                lb.CommandName = "ApproveVacation";
                lb.Command += LinkButton_Command;
                row.Cells[0].Controls.Add(lb);
            }
        }*/
    }
}